<?php 
namespace Admin\Model;

class RuleModel extends CommonModel{
	protected $_fields = ["rule_name","parent_id","module_name","controller_name","action_name","is_shop"];
	protected $_validate = [
		array("rule_name","require","权限名称必填"),
		array("module_name","require","模型名称必填"),
		array("controller_name","require","控制器名称必填"),
		array("action_name","require","方法名称必填")
	];
	public function getParentTree($id=0){
		//获取格式化的权限
		$data = $this->select();
		$list = $this->getTree($data,$id);
		return $list;
	}
	public function getTree($data,$id=0,$lev=1,$is_static=false){
		static $list = [];
		if($is_static){
			$list = [];
		}
		foreach ($data as $key => $value) {
			if($value["parent_id"] == $id){
				$value["lev"] = $lev;
				$list[] = $value;
				$this->getTree($data,$value["id"],$lev+1);
			}
		}
		return $list;
	}
	public function dels($id){
		//有子权限不许删除
		$res = $this->where("parent_id=".$id)->find();
		if($res){
			return false;
		}
		return $this->where("id=".$id)->delete();
	}
	public function update($data){
		//不许父权限修改到子权限
		//获取所有的子权限
		$seed_parent = $this->getParentTree($data["id"]);
		foreach ($seed_category as $key => $value) {
			if($data["id"] == $seed_category["parent_id"]){
				return false;
			}
		}
		return $this->save($data);
	}
}